package com.plane;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class Hero extends Sprite {
    boolean up, down, left, right;

    public Hero() {
        this.width = 60;
        this.height = 76;
        x = (Map.MAP_WIDTH - width) / 2;
        y = Map.MAP_HEIGHT - height;
        this.xStep = 5;
        this.yStep = 5;

        try {
            image = ImageIO.read(Hero.class.getResourceAsStream("plane01.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void move() {
        if (up) {
            y -= yStep;
            if (y < 0) {
                y = 0;
            }
        }
        if (down) {
            y += yStep;
            if (y > Map.MAP_HEIGHT - height) {
                y = Map.MAP_HEIGHT - height;
            }
        }
        if (left) {
            x -= xStep;
            if (x < 0) {
                x = 0;
            }
        }
        if (right) {
            x += xStep;
            if (x > Map.MAP_WIDTH - width) {
                x = Map.MAP_WIDTH - width;
            }
        }
    }

    public void paint(Graphics gTmp, GamePanel gp) {
        gTmp.drawImage(image, x, y, gp);
    }

    public void pressed(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_W:
                up = true;
                break;
            case KeyEvent.VK_S:
                down = true;
                break;
            case KeyEvent.VK_A:
                left = true;
                break;
            case KeyEvent.VK_D:
                right = true;
                break;
        }
    }

    public void released(int keyCode) {
        switch (keyCode) {
            case KeyEvent.VK_W:
                up = false;
                break;
            case KeyEvent.VK_S:
                down = false;
                break;
            case KeyEvent.VK_A:
                left = false;
                break;
            case KeyEvent.VK_D:
                right = false;
                break;
        }
    }

}
